class PostsController < ApplicationController
  before_filter :set_nav_session
  before_filter :login_required
  
  def create
    @group = Group.find(params[:group_id])
    @topic = Topic.find(params[:topic_id])
    post = Post.new(params[:post])
    post.user = current_user
    @topic.posts << post
    flash[:notice] = "你回复了\"#{@topic.title}\""
    redirect_back_or_default(topic_url(@group.id, @topic.id))
  end

  def edit
    @post = Post.find(params[:id])
    @topic = @post.topic
    @group = @topic.group
  end
  
  def update
    @post = Post.find(params[:id])
    @post.update_attributes!(params[:post])
    flash[:notice] = "修改了自己的回复"
    redirect_back_or_default(topic_url(params[:group_id], params[:topic_id]))
  end
  
  def destroy
    @post = Post.find(params[:id])
    @post.destroy
    flash[:notice] = "删除了自己的回复"
    redirect_back_or_default(topic_url(params[:group_id], params[:topic_id]))
  end
  
  private
  def set_nav_session
    session[:nav] = "groups"
  end
end
